//引入请求文件
var util = require('../../utils/util.js');
var qiniuUploader = require('../../utils/qiniuUploader.js');
let app = getApp();
let imgUrl = app.imgUrl;
let url = app.url;
let gobalData = app.gobalData;
Page({
	data: {
		userAvatar: '',
		explain: "",
		images: [],
		navData: [],
		labelList: [],
		furnitureId: 0,
		currentTab: 0,
		navScrollLeft: 0,
		date: "今天",
		Price: "",
		cid: "",
		submitFlag: false,
		subLoading: false // loading
	},
	clicks: function(e) {
		let index = e.currentTarget.dataset.index; //获取你点击的索引下标
		let arrs = this.data.labelList;
		arrs.forEach((item, i) => {
			arrs[i].status = 0;
		})
		arrs[index].status = 1;
		this.setData({
			labelList: arrs
		})
	},
	bindDateChange: function(e) {
		this.setData({
			date: e.detail.value
		})
	},
	//事件处理函数
	onLoad: function() {
		// 获取用户头像以及昵称
		this.setData({
			userName: wx.getStorageSync('userInfo').userInfo.name,
			userAvatar: wx.getStorageSync('userInfo').userInfo.avatar,
			cid: wx.getStorageSync('userInfo').userInfo.id
		})
		// 获取接口信息---装修tab列表
		util(`${url}Furniture/getFurnitureClassificationList`).then(res => {
			if (res.status == 1) {
				this.setData({
					navData: res.content.data
				})
			} else {
				wx.showToast({
					title: "获取数据失败",
					icon: "none"
				})
			}
		})
		// 获取tab列表详情
		util(`${url}Furniture/getFurnitureList`, {
			furnitureClassificationId: 4
		}).then(res => {
			if (res.status == 1) {
				this.setData({
					labelList: res.content.data
				})
			} else {
				wx.showToast({
					title: "获取数据失败",
					icon: "none"
				})
			}
		})
	},
	// 获取单选数据
	radioLabel(e) {
		let arr = this.data.labelList;
		let furnitureId = this.data.furnitureId;
		arr.forEach(item => {
			if (item.furnitureName == e.detail.value) {
				furnitureId = item.id
			}
		})
		this.setData({
			furnitureId
		})
	},
	// 获取金额数据
	getPrice(e) {
		this.setData({
			Price: e.detail.value
		})
	},
	// 获取备注数据
	getExplain(e) {
		this.setData({
			explain: e.detail.value
		})
	},
	// 提交表单
	formSubmit: function(e) {
		this.setData({
			subLoading: true
		})
		if (this.data.submitFlag == false) {
			// 上传图片
			let arr = [];
			let self = this;
			let obj = {
				cid: this.data.cid,
				date: this.data.date,
				price: this.data.Price,
				consumerGoods: this.data.explain,
				furnitureId: this.data.furnitureId,
				picture: this.data.images
			}
			console.log('提交的数据：', obj)
			let upLoad = (img) => {
				return new Promise((resolve, reject) => {
					qiniuUploader.upload(img, (res) => {
						resolve(res.imageURL);
					}, (error) => {
						reject(error)
					}, {
						domain: imgUrl,
						region: 'ECN',
						uptokenURL: `${url}Upload/getUpToken`
					})
				})
			}

			switch (this.data.images.length) {
				case 1:
					upLoad(self.data.images[0]).then(res => {
						obj.picture = res;
						util(`${url}Billing/addFurniture`, obj).then(res => {
							if (res.status == 1) {
								this.setData({
									subLoading: false,
									submitFlag: true
								})
								wx.navigateBack({
									url: '/pages/renovationBill/index'
								})
							} else {
								this.setData({
									subLoading: false,
									submitFlag: false
								})
								wx.showToast({
									title: res.msg,
									icon: "none"
								})
							}
						})
					})
					break;
				case 2:
					upLoad(self.data.images[0]).then(res => {
						arr.push(res);
						return upLoad(self.data.images[1]);
					}).then(res => {
						arr.push(res);
						obj.picture = arr;
						util(`${url}Billing/addFurniture`, obj).then(res => {
							if (res.status == 1) {
								this.setData({
									subLoading: false,
									submitFlag: true
								})
								wx.navigateBack({
									url: '/pages/renovationBill/index'
								})
							} else {
								this.setData({
									subLoading: false,
									submitFlag: false
								})
								wx.showToast({
									title: res.msg,
									icon: "none"
								})
							}
						})
					})
					break;
				case 3:
					upLoad(self.data.images[0]).then(res => {
						arr.push(res);
						return upLoad(self.data.images[1]);
					}).then(res => {
						arr.push(res);
						return upLoad(self.data.images[2]);
					}).then(res => {
						arr.push(res);
						obj.picture = arr;
						util(`${url}Billing/addFurniture`, obj).then(res => {
							if (res.status == 1) {
								this.setData({
									subLoading: false,
									submitFlag: true
								})
								wx.navigateBack({
									url: '/pages/renovationBill/index'
								})
							} else {
								this.setData({
									subLoading: false,
									submitFlag: false
								})
								wx.showToast({
									title: res.msg,
									icon: "none"
								})
							}
						})
					})
					break;
				default:
					util(`${url}Billing/addFurniture`, obj).then(res => {
						if (res.status == 1) {
							this.setData({
								subLoading: false,
								submitFlag: true
							})
							wx.navigateBack({
								url: '/pages/renovationBill/index'
							})
						} else {
							this.setData({
								subLoading: false,
								submitFlag: false
							})
							wx.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					})
			}
		} else {
			return false;
		}


	},
	// 再记一笔
	againSubmit() {

		// 上传图片
		let arr = [];
		let self = this;
		let obj = {
			cid: this.data.cid,
			date: this.data.date,
			price: this.data.Price,
			consumerGoods: this.data.explain,
			furnitureId: this.data.furnitureId,
			picture: this.data.images
		}
		let upLoad = (img) => {
			return new Promise((resolve, reject) => {
				qiniuUploader.upload(img, (res) => {
					resolve(res.imageURL);
				}, (error) => {
					reject(error)
				}, {
					domain: imgUrl,
					region: 'ECN',
					uptokenURL: `${url}Upload/getUpToken`
				})
			})
		}

		switch (this.data.images.length) {
			case 1:
				upLoad(self.data.images[0]).then(res => {
					obj.picture = res;
					util(`${url}Billing/addFurniture`, obj).then(res => {
						if (res.status == 1) {
							wx.showToast({
								title: "记录成功！"
							})
							this.setData({
								explain: "",
								date: "今天",
								Price: ""
							})
						} else {
							wx.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					})
				})
				break;
			case 2:
				upLoad(self.data.images[0]).then(res => {
					arr.push(res);
					return upLoad(self.data.images[1]);
				}).then(res => {
					arr.push(res);
					obj.picture = arr;
					util(`${url}Billing/addFurniture`, obj).then(res => {
						if (res.status == 1) {
							wx.showToast({
								title: "记录成功！"
							})
							this.setData({
								explain: "",
								date: "今天",
								Price: ""
							})
						} else {
							wx.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					})
				})
				break;
			case 3:
				upLoad(self.data.images[0]).then(res => {
					arr.push(res);
					return upLoad(self.data.images[1]);
				}).then(res => {
					arr.push(res);
					return upLoad(self.data.images[2]);
				}).then(res => {
					arr.push(res);
					obj.picture = arr;
					util(`${url}Billing/addFurniture`, obj).then(res => {
						if (res.status == 1) {
							wx.showToast({
								title: "记录成功！"
							})
							this.setData({
								explain: "",
								date: "今天",
								Price: ""
							})
						} else {
							wx.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					})
				})
				break;
			default:
				util(`${url}Billing/addFurniture`, obj).then(res => {
					if (res.status == 1) {
						wx.showToast({
							title: "记录成功！"
						})
						this.setData({
							explain: "",
							date: "今天",
							Price: ""
						})
					} else {
						wx.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
		}
	},
	// 滑动效果
	switchNav(event) {
		// 获取tab列表详情
		util(`${url}Furniture/getFurnitureList`, {
			furnitureClassificationId: this.data.navData[event.currentTarget.dataset.current].id
		}).then(res => {
			if (res.status == 1) {
				this.setData({
					labelList: res.content.data
				})
			} else {
				wx.showToast({
					title: "获取数据失败",
					icon: "none"
				})
			}
		})
		var cur = event.currentTarget.dataset.current;
		//每个tab选项宽度占1/4
		var singleNavWidth = this.data.windowWidth / 4;
		//tab选项居中                            
		this.setData({
			navScrollLeft: (cur - 2) * singleNavWidth
		})
		if (this.data.currentTab == cur) {
			return false;
		} else {
			this.setData({
				currentTab: cur
			})
		}
	},
	switchTab(event) {
		// 获取tab列表详情
		util(`${url}Furniture/getFurnitureList`, {
			furnitureClassificationId: this.data.navData[event.detail.current].id
		}).then(res => {
			if (res.status == 1) {
				this.setData({
					labelList: res.content.data
				})
			} else {
				wx.showToast({
					title: "获取数据失败",
					icon: "none"
				})
			}
		})
		var cur = event.detail.current;
		var singleNavWidth = this.data.windowWidth / 4;
		this.setData({
			currentTab: cur,
			navScrollLeft: (cur - 2) * singleNavWidth
		});
	},
	// 图片展示
	chooseImage(e) {
		wx.chooseImage({
			count: 3,
			sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
			sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
			success: res => {
				const images = res.tempFilePaths
				// 限制最多只能留下3张照片
				this.setData({
					images: images
				})
			}
		})
	},
	// 判断用户选择删除还是预览
	handleImagePreview(e) {
		wx.showModal({
			title: '删除，预览',
			content: '删除当前图片，预览当前图片',
			cancelText: '删除',
			cancelColor: '#fa863d',
			confirmText: '预览',
			confirmColor: '#fa863d',
			success: res => {
				// 预览
				if (res.confirm) {
					const idx = e.target.dataset.idx
					const images = this.data.images
					wx.previewImage({
						current: images[idx], //当前预览的图片
						urls: images, //所有要预览的图片
					})
				} else { //删除
					const idx = e.target.dataset.idx
					this.data.images.splice(idx, 1);
					this.setData({
						images: this.data.images
					})
				}
			}
		})
	}

})
